package io.jee.runshan.controller;

import io.jee.runshan.handler.WeixinReceiverMemberHandler;
import io.jee.runshan.handler.WeixinReceiverServerHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/weixin/")
public class WeixinReceiver {
	private static final Logger logger = LoggerFactory.getLogger(WeixinReceiver.class);

	@RequestMapping("receive")
	public void receive(HttpServletRequest request, HttpServletResponse response) throws Exception {
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		response.setContentType("application/xml");

		String method = request.getMethod();

		logger.debug("current Weixin request: " + method);
		// 微信服务器将发送GET请求到填写的URL上,这里需要判定是否为GET请求
		if (method.toUpperCase().equals("GET")) {
			WeixinReceiverServerHandler hander = new WeixinReceiverServerHandler();
			hander.receiver(request, response);
		} else {
			WeixinReceiverMemberHandler hander = new WeixinReceiverMemberHandler();
			hander.receiver(request, response);
		}
	}

}
